package com.lottery.bootdo.common.controller.events;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lottery.bootdo.common.service.EventsInfoService;
import com.lottery.bootdo.common.utils.PageUtils;
import com.lottery.bootdo.common.utils.StringUtils;
import com.lottery.common.contants.ReQueryResult;
import com.lottery.common.entity.CustomInfo;
import com.lottery.common.entity.EventsInfo;
import com.lottery.common.entity.MemberInfo;
import com.lottery.common.entity.SysConfig;
import com.lottery.common.service.DecryptToObjectService;
import com.lottery.common.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.lottery.common.contants.Constants.*;

@Controller
@RequestMapping("/recommend")
@Slf4j
@Api(value = "/recommend",tags="赛事推荐操作类")
public class RecommendController {
	private ReQueryResult result = new ReQueryResult();

	@Autowired
	private EventsInfoService eventsInfoService;
	@Autowired
	private DecryptToObjectService decryptToObjectService;


	@RequiresPermissions("recommend:list")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	String index() {
		return "common/recommend/index";
	}

	@RequiresPermissions("recommend:member:list")
	@RequestMapping(value = "/member/index", method = RequestMethod.GET)
	String memberIndex() {
		return "common/recommend/member/index";
	}

	@RequiresPermissions("recommend:memberCustom:list")
	@RequestMapping(value = "/memberCustom/index", method = RequestMethod.GET)
	String memberCustomIndex() {
		return "common/recommend/memberCustom/index";
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	@RequiresPermissions(value={"recommend:list","recommend:member:list","recommend:memberCustom:list","custom:edit"},logical= Logical.OR)
	String detail(@PathVariable() Long id, Model model) {
		EventsInfo info=eventsInfoService.getById(id);

		try{
			if (info.getType()==3){
				info.setCustomTypeStr(RedisUtils.getConfigFromRedis(info.getCustomType(), SysConfig.class).getRemark());
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		model.addAttribute("info", info);
		model.addAttribute("type", info.getType());
		return "common/recommend/detail";
	}

	@RequestMapping(value = "/detail/member/{id}", method = RequestMethod.GET)
	@RequiresPermissions("recommend:member:detail")
	String memberDetail(@PathVariable() Long id, Model model) {
		EventsInfo info=eventsInfoService.getById(id);

		try{
			if (info.getType()==3){
				info.setCustomTypeStr(RedisUtils.getConfigFromRedis(info.getCustomType(), SysConfig.class).getRemark());
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		model.addAttribute("info", info);
		model.addAttribute("type", info.getType());
		return "common/recommend/member/detail";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	@RequiresPermissions("recommend:edit")
	String edit( @PathVariable() Long id,Integer type, Model model) {
		EventsInfo info=eventsInfoService.getById(id);
		if (info==null){
			info=new EventsInfo();
		}
		model.addAttribute("info", info);
		model.addAttribute("type",type);
		return "common/recommend/edit";
	}

	@RequestMapping(value = "/edit/member/{id}", method = RequestMethod.GET)
	@RequiresPermissions("recommend:member:edit")
	String memberEdit( @PathVariable() Long id,Integer type, Model model) {
		EventsInfo info=eventsInfoService.getById(id);
		model.addAttribute("info", info);
		model.addAttribute("type",type);

		return "common/recommend/member/edit";
	}



	@RequestMapping(value = "", method = RequestMethod.GET)
	@RequiresPermissions(value={"recommend:list","recommend:member:list","recommend:memberCustom:list","custom:edit"},logical= Logical.OR)
	@ResponseBody
	public PageUtils recommendAll(String json){
		log.info("__________ recommendAll");
		result = new ReQueryResult();
		NutMap nutMap=decryptToObjectService.jsonToObject(json,NutMap.class);
		log.info("nutmap is {}", JSON.toJSONString(nutMap));

		QueryWrapper<EventsInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("type",nutMap.getInt("type")).eq("is_del",TYPE_ONE);
		if (StringUtils.isNotBlank(nutMap.getString("id")))
			queryWrapper.like("id",nutMap.get("id"));
		if (StringUtils.isNotBlank(nutMap.getString("name")))
			queryWrapper.like("name",nutMap.get("name"));
		if (StringUtils.isNotBlank(nutMap.getString("eventsType")))
			queryWrapper.eq("events_type",nutMap.getInt("eventsType"));
		if (StringUtils.isNotBlank(nutMap.getString("phone")))
			queryWrapper.like("phone",nutMap.get("phone"));
		if (StringUtils.isNotBlank(nutMap.getString("startTime"))){
			queryWrapper.apply("  create_time > '"+nutMap.getString("startTime")+"'");
		}
		if (StringUtils.isNotBlank(nutMap.getString("endTime"))){
			queryWrapper.apply("  create_time < '"+nutMap.getString("endTime")+"'");
		}

		queryWrapper.orderByDesc("id");
		Page<EventsInfo> page = new Page<>(nutMap.getInt("pageIndex"),nutMap.getInt("pageLength"));

		IPage<EventsInfo> records=eventsInfoService.page(page,queryWrapper);

		try{
			if (nutMap.getInt("type")==3){
				for(EventsInfo info:records.getRecords()){
					info.setCustomTypeStr(RedisUtils.getConfigFromRedis(info.getCustomType(), SysConfig.class).getRemark());
				}
			}

		}catch (Exception e){
			e.printStackTrace();
		}
//		log.info(JSON.toJSONString(records));
		PageUtils pageUtils = new PageUtils(records.getRecords(),new BigDecimal(records.getTotal()).intValue());
		return pageUtils;
	}


	@RequestMapping(value = "", method = RequestMethod.POST)
	@RequiresPermissions(value={"recommend:edit","recommend:member:edit","recommend:memberCustom:list"},logical= Logical.OR)
	@ResponseBody
	ReQueryResult saveOrUpdate( EventsInfo info) {
		log.info("__________ update");
		result = new ReQueryResult();
		if (info.getEventsType()==null)
			info.setEventsType(TYPE_ONE);
		eventsInfoService.saveOrUpdate(info);
		result.success(SUCCESS_DESC);
		return result;
	}


	@RequestMapping(value = "/hot", method = RequestMethod.POST)
	@RequiresPermissions(value={"recommend:hot"},logical= Logical.OR)
	@ResponseBody
	ReQueryResult hot( Long id,Integer hot, Model model) {
		log.info("__________ hot");
		result = new ReQueryResult();
		EventsInfo info=new EventsInfo();
		info.setId(id);
		info.setIsHot(hot);
		eventsInfoService.updateById(info);
		result.success(SUCCESS_DESC);
		return result;
	}


	@RequestMapping(value = "", method = RequestMethod.DELETE)
	@RequiresPermissions(value={"recommend:del","recommend:member:del","recommend:memberCustom:list"},logical= Logical.OR)
	@ResponseBody
	ReQueryResult delete( EventsInfo info) {
		log.info("__________ delete");
		result = new ReQueryResult();
		info.setIsDel(TYPE_ZERO);
		eventsInfoService.updateById(info);
		result.success(SUCCESS_DESC);
		return result;
	}


	@RequestMapping(value = "/batchDel", method = RequestMethod.DELETE)
	@RequiresPermissions(value={"recommend:del","recommend:member:del","recommend:memberCustom:list"},logical= Logical.OR)
	@ResponseBody
	public ReQueryResult batchDelete(@RequestParam("ids[]")Long[] ids){
		log.info("__________batchDel");
		result = new ReQueryResult();
		List<EventsInfo> list=new ArrayList<>();
		EventsInfo info;
		for (Long id:ids){
			info=new EventsInfo();
			info.setId(id);
			info.setIsDel(TYPE_ZERO);
			list.add(info);
		}
		eventsInfoService.updateBatchById(list);
		result.success(SUCCESS_DESC);
		return result;
	}
}
